define(['jquery','jquery_ext','config','layer'], function($,jqext,config,layer) {
	
	Array.prototype.indexOf = function(val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
    Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
	
	//加载扩展模块
    layer.config({
		extend: '../plugins/layer/extend/layer.ext.js'
	});
	///window.layer = layer;
	
	var _loading = function(){
		return layer.load(0, {shade: 0.3});
	};
	//window.loading = _loading;
	
	//window.Common = {};
	
	/**
	 * 弹出消息提示
	 */
	_alert = function(msg){
		//过滤业务码
		layer.msg(rep_businessNo(msg));
	};
	
	/**
	 * 跳转登录
	 */
	_toLogin = function(){
		if(window.location.href.indexOf(config.webRoot()+"/html/login.html")<0){
			window.location.href = config.webRoot()+"/html/login.html?url="+encodeURIComponent(window.location.href);
		}
	};
	//注册到 window
	//window.toLogin = Common.toLogin;
	_ajax_request2 = function($type,$url,$params,$callback,$async){
		//判空
		if($type == null || typeof $type === 'undefined' || $url == null ||typeof $url === 'undefined'){
			_alert('参数错误: $type , $url 是非空字段');
			return;
		}
		//如果参数为空设置为空对象
		if($params == null ||typeof $params === 'undefined' || typeof $params != 'object' ){
			$params={};
		}
		//异步加载判断
		var _async=true;
		if(typeof $async === 'boolean'){
			_async=$async;
		}
		
		$.ajax({
			url: config.apiRoot()+$url,
			type: $type,
			data: config.setFullParameter($params),
			dataType: 'jsonp',
			cache: 'false',
			async:_async,
			success: function($resultdata) {
				//console.info($resultdata);
				if($resultdata!=null && $resultdata!=''){
					if($resultdata.code=='401'){//未登录
						_toLogin();
					}else{
						var _alertbusiness=true;
						if(typeof $callback ==='function'){
							var _callbackobj={
									$data:$resultdata.data,
									$business:$resultdata.businessMsg,
									$code:$resultdata.code
								};
							$.funParameterBind($callback,_callbackobj,{
								$business:function(){ _alertbusiness=false;}
							},function($exec){
								// 没有业务变量那么 code=200 的时候执行该方法否则执行提示
								$exec();
								if(_alertbusiness && $resultdata.code != '200'){
									_alert($resultdata.businessMsg.businessNote);
								}
							});
						}
					}
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				//TODO:隐藏弹出框
				//TODO:提示消息
				if(jqXHR.readyState === 0 && jqXHR.status === 0){}else if(jqXHR.status != '200'){
					var _showerror=true;
					$.funParameterBind($callback,{
						$error:jqXHR
					},{
						$error:function(){_showerror=false;}
					});
					if(_showerror){
//						_alert('请求:'+$url+' \r\n错误{ status:'+jqXHR.status+'  ,statusText:'+jqXHR.statusText+'}');
						_alert("系统异常");
					}
				}
			}
		});
	};
	//注册到 window
	///window.ajax_request2=Common.ajax_request2;
	/*var _request=function($gtype,$url,$params,$callback,$businesscallback400,$jump401){
		//判空
		if($gtype == null || typeof $gtype === 'undefined' || $url == null ||typeof $url === 'undefined'){
			_alert('参数错误: gtype , url 是非空字段');
			return;
		}
		//如果参数为空设置为空对象
		if($params == null ||typeof $params === 'undefined' || typeof $params != 'object' ){
			$params={};
		}
		
		$.ajax({
			url: config.apiRoot()+$url,
			type: $gtype,
			data: config.setFullParameter($params),
			dataType: 'jsonp',
//			jsonp: "callback",
//			contentType: "application/json;charset=UTF-8",
			cache: 'false',
			success: function($resultdata) {
				if($resultdata!=null){
					if($resultdata.code=='200'){
						//业务正常
						if($callback !=null && typeof $callback === 'function'){
							//data回调
							$callback($resultdata.data,$resultdata.code,$resultdata.businessMsg);
						}
					}else if($resultdata.code=='401'){//未登录
						if(typeof $jump401 === 'function'){
							$jump401($resultdata.code,$resultdata.businessMsg);
						}else{
							_toLogin();
						}
					}else if($resultdata.code != '400'){//业务错误
						//业务异常回调
						if($businesscallback400 !=null && typeof $businesscallback400 === 'function'){
							$businesscallback400($resultdata.code,$resultdata.businessMsg);
						}else if($businesscallback400 !=null && typeof $businesscallback400 === 'string' && $businesscallback400 === 'sys'){
							//未定义业务接收 方法
							layer.msg($resultdata.businessMsg.businessNote);
						}
					}else{//未定义业务接收 方法
						layer.msg($resultdata.businessMsg.businessNote);
					}
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				//TODO:隐藏弹出框
				//TODO:提示消息
				if(jqXHR.readyState === 0 && jqXHR.status === 0){}else{
					//Common.alert("请求错误:"+JSON.stringify(jqXHR) + "," + textStatus + "," + errorThrown);
					_alert("系统异常");
				}
			}
		});
	};*/
	
	//注册到window
	//window.ajax_request=_request;

	
	//鼠标经过移开
	$(document).on('mouseenter','.memolink',function(){
		$(this).popover('show');
	});
	$(document).on('mouseleave','.memolink',function(){
		$(this).popover('hide');
	});
	
	return {
		/**
		 * ajax 请求
		 * 例子 : common.ajax_request('get',config.apiRoot()+'/api/seller/v1/menu_config',{},function(data){},function(data){});
		 * 
		 * @param $gtype 请求类型 string  (GET | POST)
		 * @param $url  请求地址 string  (http://xxx.xxx.xxx/)
		 * @param $params  请求参数 object  ($('xxx').serializeArray())
		 * @param $callback  请求回调 function(data,code,businessMsg);
		 * @param $businesscallback400  未处理业务回调 function(code,businessMsg); | sys(string 系统弹出)
		 * @param $jump401  未设置自动跳转 function(code,businessMsg);
		 * @returns
		 */
		//ajax_request:_request($gtype,$url,$params,$callback,$businesscallback400,$jump401),
		/*{
			return _request($gtype,$url,$params,$callback,$businesscallback400,$jump401);
		},*/
		ajax_request2:_ajax_request2,
		/*common_ajax_request:function(action, params, care, callback,$jump401,$gtype,$complete){
			return _common_request(action, params, care, callback,$jump401,$gtype,$complete);
		},*/
		loading:_loading,
		alert:_alert,
		toLogin : _toLogin
	};
});
